using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using Microsoft.SharePoint;
using System.Diagnostics;

namespace CKS.EBE.WebControls
{
    public enum CacheScope
    {
        User = 0x0001,
        Page = 0x0002,
        Web = 0x0004,
        Site = 0x0008,
        Groups = 0x0010,
        ContentType = 0x0020,
        QueryString = 0x0040,
        Undefined = 0
    }

    public class EBECache
    {
        public string KeySuffix = "";
        public string SiteKey = null;
        public string Key = "";
        public int Timeout = 30;
        public System.Web.Caching.CacheItemPriority Priority = System.Web.Caching.CacheItemPriority.Normal;

        public EBECache(string sCacheKey) { Key = sCacheKey; }

        private Nullable<CacheScope> _scopeHint = CacheScope.Page;
        private Nullable<bool> _enabledHint = null;

        public bool IsCached
        {
            get
            {
                // Don't cache for Admins!!
                if (SPContext.Current.Web.UserIsWebAdmin) return false;

                return (HttpContext.Current.Cache[CacheKey] != null);
            }
        }

        public object Value
        {
            get
            {
                return HttpContext.Current.Cache[CacheKey];
            }

            set
            {
                HttpContext.Current.Cache.Add(CacheKey, value, null, DateTime.Now.AddSeconds(Timeout), System.Web.Caching.Cache.NoSlidingExpiration, Priority, null);
            }
        }

        public void Invalidate()
        {
            HttpContext.Current.Cache.Remove(CacheKey);
        }

        protected string CacheKey
        {
            get
            {
                if (!string.IsNullOrEmpty(SiteKey)) return SiteKey;

                string sKey = Key + "({0})" + KeySuffix;

                return string.Format(sKey, ScopeKey()); 
            }
        }

        protected string ScopeKey()
        {
            string sScope = "";
            try
            {
                if ((Scope & CacheScope.ContentType) != 0)
                    sScope += SPContext.Current.ListItem.ContentType.Name;

                if ((Scope & CacheScope.QueryString) != 0)
                    sScope += HttpContext.Current.Request.QueryString.ToString();

                else if ((Scope & CacheScope.Web) != 0)
                    sScope = SPContext.Current.Web.Url;

                else if ((Scope & CacheScope.Groups) != 0)
                    sScope = SPContext.Current.Web.CurrentUser.Groups.Xml;

                else if ((Scope & CacheScope.Site) != 0)
                    sScope = SPContext.Current.Site.Url;

                else if ((Scope & CacheScope.Page) != 0)
                    sScope = HttpContext.Current.Request.Url.PathAndQuery;

                if ((Scope & CacheScope.User) != 0)
                    sScope += HttpContext.Current.User.Identity.Name;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex);
            }

            //Debug.WriteLine("Cache ScopeKey: " + sScope);

            //Undefined
            return sScope;
        }

        public CacheScope Scope
        {
            get 
            {
                if (_scopeHint != null) return (CacheScope)_scopeHint;

                return CacheScope.Undefined;
            }
            set 
            {
                _scopeHint = value;
            }
        }

        public bool Enabled
        {
            get
            {
                if (_enabledHint != null) return (bool)_enabledHint;

                return false;
            }

            set
            {
                _enabledHint = value;
            }
        }

    }
}
